﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Md.Demo.Tools
{
    public static class ExtensionMethod
    {
        public static string ToJson<T>(this T obj)
        {
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter
            {
                DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
            };
            return JsonConvert.SerializeObject(obj, timeFormat, new StringEnumConverter());
        }
        public static T ToObject<T>(this string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }

        public static IQueryable<T> GetPage<T>(this IQueryable<T> objlist, int pageIndex, int pageSize) where T : class
        {
            return objlist.Skip((pageIndex - 1) * pageSize).Take(pageSize).AsQueryable();
        }
        public static IEnumerable<T> GetPage<T>(this IEnumerable<T> objlist, int pageIndex, int pageSize) where T : class
        {
            return objlist.Skip((pageIndex - 1) * pageSize).Take(pageSize);
        }
    }
}
